/**
 * @(#) HouseRepayProxy.as
 *
 * @date		2013
 * @version		1.0
 * @author		Jo,Chang Hun
 * @description	 연말정산  - Proxy
 * 				 
 */
package com.skt.common_hr.yearadjustment.model
{
	
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.common.puremvc.DataReceiver;
	import com.skt.common_hr.yearadjustment.model.vo.HouseRepayVO;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class HouseRepayProxy extends CommonProxy
	{
		public static const NAME:String = "HouseRepayProxy";
		
		public static const LOAD_HOUSE_REPAY_COMPLETE:String = "loadHouseFundComplete";
		public static const LOAD_HOUSE_REPAY_FAILED:String = "loadHouseFundFailed";
		public static const SAVE_HOUSE_REPAY_COMPLETE:String = "saveHouseFundComplete";
		public static const SAVE_HOUSE_REPAY_FAILED:String = "saveHouseFundFailed";
		public static const LOAD_COMBO_TYPE_HOUSE_COMPLETE:String = "LOAD_COMBO_TYPE_HOUSE_COMPLETE";
		
		
		public var msgStr:String;
		public var houseRepayList:ArrayCollection = new ArrayCollection();
		

		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function HouseRepayProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			//CursorManager.setBusyCursor();
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0085");
			
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = pernr;
			param["I_YEAGBN"] = "E6";		//01:보험비, 02:교육비, 03:시뇽카드 비용, 04: 현금 비용, 05:지로 비용, 06:기부금, 07:의료비
			param["I_PROC_TYPE"] = "D";		//D:조회, S:저장
			loadData("Z_HR_0085_NEW", param, result, fault);
		}
		
		public function save(parnr:String, year:String, donationArr:Array):void{
			//CursorManager.setBusyCursor();
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0085");
			/* for(var i:int  = 0 ; i < donationArr.length; i++){
				trace("YEAGBN : " + donationArr[i].YEAGBN);
				trace("NATCT : " + donationArr[i].NATCT);
				trace("NATAM : " + donationArr[i].NATAM);
				trace("REGNO : " + donationArr[i].REGNO);
				trace("COMCD : " + donationArr[i].COMCD);
			} */
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = parnr;
			param["I_YEAGBN"] = "E6";		//01:보험비, 02:교육비, 03:시뇽카드 비용, 04: 현금 비용, 05:지로 비용, 06:기부금, 07:의료비
			param["I_PROC_TYPE"] = "S";		//D:조회, S:저장
			var table:Object = new Object();
			table["ITAB3"] = donationArr;
			saveData("Z_HR_0085_NEW", param, table, saveResult, saveFault);
		}
		
		//		2014
		public function typeload():void
		{
			var param:Object = new Object();
			param["I_YEAGBN"] = "E6";			

			loadMultiData("Z_HR_0096_NEW", param, typeResult); 
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				houseRepayList = parseHouseRepay(event.result.ITAB3);
				facade.sendNotification(LOAD_HOUSE_REPAY_COMPLETE, this);
			}
			else {
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(LOAD_HOUSE_REPAY_FAILED, this);
			}
		}

		public var comboInType:ArrayCollection = new ArrayCollection();
		public function typeResult(event:DataReceiver):void
		{
			if(retCode==0) {  
				comboInType = event.result.ITAB;
				facade.sendNotification(HouseRepayProxy.LOAD_COMBO_TYPE_HOUSE_COMPLETE, this);
			}
			else {
				msgStr = event.result["RETTEXT"];
				Alert.show(msgStr);
			}
		}		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			msgStr = event.fault.faultString;
			facade.sendNotification(LOAD_HOUSE_REPAY_FAILED, this);
		}
		
		//저장 완료
		public function saveResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				facade.sendNotification(SAVE_HOUSE_REPAY_COMPLETE, this);
			}
			else {
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(SAVE_HOUSE_REPAY_FAILED, this);
			}
		}
		
		//저장실패
		private function saveFault(event:FaultEvent):void
		{
			msgStr = event.fault.faultString;
			facade.sendNotification(SAVE_HOUSE_REPAY_FAILED, this);
		}
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 연말정산  MainList Value Object
		 */
		/**
		 * 개인정보
		 * 
		 * @param
		 * @return 특별공제 보험료 Value Object
		 */
		private function parseHouseRepay(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var obj:Object in recArr){						
				var retObj:HouseRepayVO = new HouseRepayVO();
				for(var field:String in obj) {			
					if(retObj.hasOwnProperty(field)==false ) {
						continue;
					}
							
					if(field == "LSBEG" || field == "LSEND" || field == "BEGDA" || field == "ENDDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);						
//						retObj[field+'_F'] = DateUtil.toDefaultString(retObj[field]);
//						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field]);											
					}
					else if(field == "TEST") {
						retObj[field] = Math.round(obj[field]*100);
					}							
					else {
						retObj[field] = obj[field];
					}					
				} 
				retArr.addItem(retObj);				
			}
			return retArr;
			
//			var returnArr:ArrayCollection = new ArrayCollection();
//			for each(var rec:Object in arr){
//				var vo:HouseRepayVO = new HouseRepayVO();
//				vo.BEGDA	= DateUtil.stringToDateTime(rec["BEGDA"]);
//				vo.ENDDA	= DateUtil.stringToDateTime(rec["ENDDA"]);
//				//vo.BETRG	= rec["BETRG"];
//				returnArr.addItem(vo);
//			}
			
			return returnArr;
		}
		
	}
}